// import moduleName from 'module'

import { getAllRole, getUserRole } from '../../../Api/rightsmanage/index'
import { getAllPermission } from '../../../Api/rightsmanage/permission'
import {getEnterpriseStaffInfo} from '../../../Api/rightsmanage/enterprise '
const roleInfo = {
    namespaced: true,
    state: () => ({
        //角色信息数组
        roleInfoList: [],           //企业所有角色信息
        allPermission: [],           //企业所有权限信息
        userPermissions: [],          //获取登录用户的角色权限
        staffInfo:[],                //企业员工信息
    }),
    mutations: {
        //保存角色信息
        setRoleInfo(state, e) {
            state.roleInfoList = e;
        },
        setAllPermission(state, e) {
            state.allPermission = e;
        },
        //保存登录用户所拥有的权限信息
        setUserPermission(state, e) {
            state.userPermissions = e;
        },
        //保存企业员工信息
        setStaffInfo(state,e){
            state.staffInfo = e;
        }
    },
    getters: {
        getUserPermissions(state) {
            return state.userPermissions
        }
    },
    actions: {
        getRoleInfo(context) {
            return new Promise((resolve, reject) => {
                getAllRole().then(res => {
                    context.commit('setRoleInfo', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        getAllPermissionInfo(context) {
            return new Promise((resolve, reject) => {
                getAllPermission().then(res => {
                    context.commit('setAllPermission', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        //异步获取登录用户权限
        getUserPermissions(context, vm) {
            return new Promise((resolve, reject) => {
                getUserRole().then(res => {
                    context.commit('setUserPermission', res.data.data)
                    resolve(res)
                    console.log('setUserPermission', res.data.data);
                    vm.$message.success('获取登录用户权限成功');
                }).catch(err => {
                    reject(err)
                })
            })
        },
        //获取企业员工信息
        getAllStaffInfo(context) {
            return new Promise((resolve, reject) => {
                getEnterpriseStaffInfo().then(res => {
                    context.commit('setStaffInfo', res.data.data)
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
    }
}

export default roleInfo